package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.maindata.domain.Factory;
import com.qijian.maindata.domain.query.FactoryQuery;
import com.qijian.maindata.domain.query.ProcessQuery;
import com.qijian.maindata.domain.vo.FactoryVo;
import com.qijian.maindata.domain.vo.ProcessVo;
import com.qijian.maindata.mapper.FactoryMapper;
import com.qijian.maindata.service.IFactoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工厂Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-19
 */
@Service
@RequiredArgsConstructor
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Factory> implements IFactoryService {

    private final FactoryMapper factoryMapper;


    @Override
    public Page<FactoryVo> pageVo(Page<Factory> page, Wrapper<FactoryQuery> queryWrapper) {
        return factoryMapper.selectPageVo(page, queryWrapper);
    }

    @Override
    public List<FactoryVo> listVo(Wrapper<FactoryQuery> queryWrapper) {
        return factoryMapper.selectPageVo(queryWrapper);
    }

    @Override
    public FactoryVo getVoById(Long id) {
        return factoryMapper.selectById(id);
    }

    @Override
    public Page<ProcessVo> processPageVo(Page<ProcessVo> page, Wrapper<ProcessQuery> queryWrapper) {
        Page<ProcessVo> processVoPage = factoryMapper.processPageVo(page, queryWrapper);
        List<ProcessVo> records = processVoPage.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for (ProcessVo record : records) {
                List<FactoryVo> factoryList = record.getProcessWorkList();
                if(CollUtil.isNotEmpty(factoryList)){
                    for (FactoryVo factoryVo : factoryList) {
                        factoryVo.setChargeName(factoryVo.getChargeUserName());
                        factoryVo.setFactoryName(factoryVo.getName());
                        factoryVo.setFactoryCode(factoryVo.getCode());
                        factoryVo.setFactoryType(factoryVo.getType());
                        factoryVo.setFactoryId(factoryVo.getId().toString());
                    }
                }

            }
        }
        return processVoPage;
    }

    @Override
    public List<FactoryVo> selectFactoryAll() {
        return baseMapper.selectFactoryAll();
    }

    @Override
    public List<String> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }

}
